package org.gitos.cloud.server.service;

import org.gitos.cloud.domain.device.Router;
import org.gitos.cloud.domain.server.Inuser;
import org.gitos.cloud.domain.server.Led;
import org.gitos.cloud.domain.server.Rom;
import org.gitos.cloud.util.CommonKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hc on 2016/3/29 0029.
 */
@Service
public class RelationService {

    @Autowired
    JdbcTemplate jdbctemplate;

    @Autowired
    Router router;

    public boolean bindInuser(String user_uuid, String mobile_os, String inuser_token) {
        List<Inuser> inusers = jdbctemplate.query("select * from inuser where user_uuid=? and inuser_token =?", new Object[]{user_uuid, inuser_token}, new Inuser());
        if (inusers.size() < 1) {
            jdbctemplate.update("insert into inuser(uuid,user_uuid,mobile_os,inuser_token) values (?,?,?,?)", CommonKit.createUuid(), user_uuid, mobile_os, inuser_token);
        }
        return true;
    }

    public List<Led> leds(String user_uuid) {
        List<Led> leds = jdbctemplate.query("select * from led where user_uuid =?", new Object[]{user_uuid}, new Led());
        for (Led led : leds) {
            led.setIs_online(router.fetchDevice(led.getMac()) != null);
        }
        return leds;
    }

    public boolean bindLed(String user_uuid, String macs) {
        if (macs == null || "".equals(macs)) {
            return false;
        }
        List<Object[]> list = new ArrayList<Object[]>();
        for (String mac : macs.split(",")) {
            Object[] para = new Object[2];
            para[0] = user_uuid;
            para[1] = mac;
            list.add(para);
        }
        try {
            jdbctemplate.batchUpdate("insert into led_user(user_uuid,mac) values (?,?)", list);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean saveLed(Led led) {
        String mac = led.getMac();
        if (mac == null || "".equals(mac)) {
            return false;
        }
        try {
            int effectedRowNum = jdbctemplate.update("update led set pub_ip=?,nat_ip=?,ssid=?,wifi_psw=?,wifi_ver=?,mcu_ver=? where mac=?", led.getPub_ip(), led.getNat_ip(), led.getSsid(), led.getWifi_psw(), led.getWifi_ver(), led.getMcu_ver(), mac);
            if (effectedRowNum < 1) {
                jdbctemplate.update("insert into led (pub_ip,nat_ip,ssid,wifi_psw,wifi_ver,mcu_ver,mac) values(?,?,?,?,?,?,?)", led.getPub_ip(), led.getNat_ip(), led.getSsid(), led.getWifi_psw(), led.getWifi_ver(), led.getMcu_ver(), mac);
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public List<Inuser> findInusersByMac(String mac) {
        return jdbctemplate.query("select * from inuser where user_uuid in (select user_uuid from led_user where mac=? )", new Object[]{mac}, new Inuser());
    }

    public List<Rom> roms(String mac, String version, int type) {
        if (StringUtils.isEmpty(version)) {
            version = "0";
        }
        return jdbctemplate.query("select * from rom where led_mac=? and version>? and type=?", new Object[]{mac, Long.parseLong(version), type}, new Rom());
    }

    public Rom getRomByUuid(String uuid) {
        List<Rom> romList = jdbctemplate.query("select * from rom where uuid =? limit 1", new Object[]{uuid}, new Rom());
        if (romList == null || romList.isEmpty()) {
            return null;
        }
        return romList.get(0);

    }
}
